//
//  NumberButtonView.swift
//  MakeMoney
//
//  Created by Fan ZhiQiang on 2024/12/10.
//

import SwiftUI

struct NumberButtonView: View {
    let number: String
    let color: Color
    var closure: () -> Void
    
    var body: some View {
        Button {
            closure()
        } label: {
            Text(number)
                .numAndColor(color: color)
        }
        .buttonStyle(.plain)
    }
}

#Preview {
    NumberButtonView(number: "10", color: .accent) {
        
    }
}

struct RenderMode: View {
    var body: some View {
        Image(systemName: "mic.badge.plus")
            .font(.system(size: 100))
            .symbolRenderingMode(.palette)//选择画板模式后改变颜色
            .foregroundStyle(.red, .orange)
            //.zIndex(-1) 默认值为0
            //.safeAreaInset(edge: <#T##VerticalEdge#>, content: <#T##() -> View#>)
            //.ignoresSafeArea()
            //.layoutPriority(1) 布局优先级默认值为0
            //.fixedSize() 固定大小
    }
}

#Preview("Render") {
    RenderMode()
}

struct AlignmentGuideView: View {
    var body: some View {
        ZStack {
            HStack(alignment: .center) {
                Text("First\nView")
                    .frame(width: 70,height: 100)
                    .background(.red)
                    .alignmentGuide(VerticalAlignment.center) { dimension in
                        dimension[VerticalAlignment.center] + 50
                    }
                    .overlay(alignment: .center) {
                        Rectangle()
                            .frame(width: .infinity, height: 5)
                            .foregroundStyle(.purple)
                    }
                Text("Second\nView")
                    .frame(width: 70,height: 80)
                    .background(.yellow)
                    .alignmentGuide(VerticalAlignment.center) { dimension in
                        dimension[VerticalAlignment.center] - 20
                    }
                    .overlay(alignment: .center) {
                        Rectangle()
                            .frame(width: .infinity, height: 5)
                            .foregroundStyle(.purple)
                    }
                Text("Third\nView")
                    .frame(width: 70,height: 120)
                    .background(.green)
    //                .alignmentGuide(VerticalAlignment.center) { dimension in
    //                    dimension[VerticalAlignment.center] + 40
    //                }
                    .overlay(alignment: .center) {
                        Rectangle()
                            .frame(width: .infinity, height: 5)
                            .foregroundStyle(.purple)
                    }
            }.border(.blue, width: 2)
            .overlay(alignment: .center) {
                Rectangle()
                    .frame(width: .infinity, height: 2)
                    .foregroundStyle(.black)
            }
            Color(.orange)
                .frame(width: .infinity, height: 1)
        }
    }
}

#Preview("Alignment") {
    AlignmentGuideView()
}

struct CustomLayoutView: View {
    @State private var slected: Bool = true
    var body: some View {
        let SelectLayout = slected ? AnyLayout(MyLayout()) : AnyLayout(VStackLayout(alignment: .leading))
        
        VStack(alignment: .leading) {
            Toggle(isOn: $slected) {
                Text(slected ? "Custom" : "System")
            }
            SelectLayout {
                Group {
                    Text("First")
                        .padding(10)
                        .background(.red)
                        .cornerRadius(10)
                    Text("Second")
                        .padding(10)
                        .background(.red)
                        .cornerRadius(10)
                    Text("Third")
                        .padding(10)
                        .background(.red)
                        .cornerRadius(10)
                }
            }
            Spacer()
        }.padding()
        .font(.title)
    }
    
    @ViewBuilder //AnyView会包装视图为任意视图但是会影响使用性能
    func getSomeView(valid: Bool) -> some View {
        if valid {
            Image(systemName: "keyboard")
        }else {
            EmptyView()//空视图不影响布局
            //Text("The state is invalid")
        }
    }
}

#Preview("Layout") {
    CustomLayoutView()
}



struct CustomButtonStyleView: View {
    @State private var color = Color.gray
    var body: some View {
        VStack {
            Text("Default Title")
                .padding()
                .foregroundStyle(color)
            Button("ChangeColro") {
                color = Color.green
            }.buttonStyle(MyButtonStyle())
            Spacer()
        }.padding()
    }
}


/**
 *  使用throws，throw，do catch
 */

enum PasswordError: Error {
    case short, obvious
}

func checkYourPassword(pswd: String) throws -> String {
    
    if pswd.count <= 5 {
        throw PasswordError.short
    }else if pswd == "123456" {
        throw PasswordError.obvious
    }
    
    return "ok pswd"
}

func doIt() {
    do {
        _ =  try checkYourPassword(pswd: "aaaa")
    } catch PasswordError.short {
        print("a")
    }catch PasswordError.obvious {
        print("b")
    }catch {
        print("c")
    }
}

/**
 *  使用closure
 */

func sayHello1(_ name: String) -> String {
    if name == "a" {
        return "chareters say hello"
    }else {
        return "\(name) say hello"
    }
}
//函数copy,让函数变成一个属性
let copy1: (String) -> String = sayHello1
//调用属性
func doIt2() {
    _ = copy1("bbb")
}
//这里不对函数进行copy了，而是直接将变量变成了一个闭包，和上面的效果一致
let sayHello2 = {(name: String) -> String in
    if name == "a" {
        return "chareters say hello"
    }else {
        return "\(name) say hello"
    }
}
//调用属性
func doIt3() {
    _ = sayHello2("bbb")
}

struct TestMystruct {
    var sayHello:(String) -> String
}

func testmYSTRUCT() {
    var sss = TestMystruct { name in
        "\(name) say hello"
    }
    var sss2 = TestMystruct(sayHello: copy1)
}



